import { useUserStore } from "@/store/modules/user";

const btnPermissions = (el, binding) => {
  let val = binding.value;
  const useUser = useUserStore();
  const permissionList = useUser.currentPermissions.value;
  const closePerm = useUser.currentPerm.value
    .filter((i) => i.value === 0)
    .map((item) => {
      return item.permissions;
    });

  //判断是否是管理员身份，且按钮控制条件包含all
  if (permissionList.includes("all") && val.includes("all")) {
    //如果当前按钮在需要关闭的权限列表中
    const index = closePerm.findIndex((i) => val.includes(i));
    if (index > -1) {
      //删除管理员对当前按钮的权限
      val = val.filter((i) => i !== "all");
    }
  }

  let flag = true;
  // 可以传入字符串和数组
  if (typeof val === "string") {
    flag = permissionList.includes(val);
  } else {
    // 如果传入数组，只要数组中任意一个值和当前permiss code或者role匹配就会显示。
    flag = val.some((item) => permissionList.includes(item));
  }
  // 如果不匹配，移除该元素
  if (!flag) el.parentNode?.removeChild(el);
};

export default btnPermissions;
